package com.fxb.doraemon.human.mapper;

import com.fxb.doraemon.human.entity.User;
import com.fxb.doraemon.human.vo.UserQueryVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户Mapper
 *
 * @author fangjiaxiaobai@gmail.com
 * @since 2019-05-07
 */
@Mapper
public interface UserMapper {

    /**
     * 根据主键查询用户
     *
     * @param id 用户的id
     * @return 用户信息
     */
    @Select("select * from t_user where id = #{id}")
    @Results({
            @Result(column = "nick_name", javaType = String.class, property = "nickName"),
            @Result(column = "name", javaType = String.class, property = "name"),
            @Result(column = "email", javaType = String.class, property = "email"),
            @Result(column = "gender", javaType = String.class, property = "gender"),
            @Result(column = "status", javaType = String.class, property = "status"),
            @Result(column = "id", javaType = Integer.class, property = "id"),
            @Result(column = "company_id", javaType = Integer.class, property = "companyId"),
            @Result(column = "post_id", javaType = Integer.class, property = "postId"),
            @Result(column = "leader_id", javaType = Integer.class, property = "leaderId"),
            @Result(column = "organization_id", javaType = Integer.class, property = "organizationId"),
            @Result(column = "emp_code", javaType = String.class, property = "empCode"),
            @Result(column = "mobile_phone1", javaType = String.class, property = "mobilePhone1"),
            @Result(column = "mobile_phone2", javaType = String.class, property = "mobilePhone2")
    })
    User select(@Param("id") String id);

    /**
     * 保存用户
     *
     * @param user 带保存的用户
     * @return 保存数据的条数据
     */
    @Insert("INSERT INTO `t_user` ( `name`, `email`, `gender`, `nick_name`, `company_id`, `post_id`, `organization_id`, `status`, `emp_code`, `leader_id`, `mobile_phone1`, `mobile_phone2`)" +
            " VALUES (#{name}, #{email}, #{gender}, #{nickName}, ${companyId}, #{postId},#{organizationId}, #{status}, #{empCode}, #{leaderId}, #{mobilePhone1}, #{mobilePhone2})")
    // 以下两种方式效果是一样的，上面的这种会有两条sql查询，下面的只有一种。
//    @SelectKey(keyProperty = "id", keyColumn = "id", statement = "SELECT LAST_INSERT_ID() as id", resultType = Integer.class, before = false)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer insert(User user);

    /**
     * 删除用户
     *
     * @param id 用户的id
     * @return 删除的个数
     */
    @Delete("delete from t_user where id = #{id}")
    Integer delete(@Param("id") String id);

    Integer update(User user);

    /**
     * 获取用户列表
     *
     * @param user 查询的条件
     * @return 查询的列表
     */
    List<UserQueryVo> selectUserFully(User user);
}
